/*****************************************************\
* Copyright (c) 2020 The game2d Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.

* Author:         dzlua - https://gitee.com/dzlua/game2d.git
* Email:          505544956@qq.com
* FileName:       core/tilemap.h
* Create Time:    2020/03/23 12:38:46
* Description:    /
\****************************************************/
#ifndef __GAME2D_CORE_TILEMAP_H__
#define __GAME2D_CORE_TILEMAP_H__

#include "sprite.h"

namespace g2d {
  struct TileMapData {
    std::string texture;
    std::vector<SDL_Rect> srcs;
    Uint32 start_id;
  };

  class TileMap : public Sprite {
    public:
      TileMap(bool load_res_on_enter) : Sprite(load_res_on_enter), level_(1) {}
      virtual ~TileMap() = default;
    public:
      virtual const Sizei& block_size() const;
      virtual void set_block_size(const Sizei &size);
      virtual const Size& tile_size() const;
      virtual void set_tile_size(const Size &size);
      virtual Result erase_tile(const char *image);
      virtual void clear_tile();
      virtual void clear_map();
      virtual void clear_level();
      virtual bool had_image(const char *id);
      virtual const char* level_file() const { return level_file_.c_str(); }
      // must have %level%: "level/level%level%.map"
      virtual Result set_level_file(const char *file);
      virtual Uint32 cur_level() const { return level_; }
      virtual void set_cur_level(Uint32 level) { level_ = level; }
      virtual void set_attribute(const char *name, const char *value);
      virtual Result load_level();
      virtual Result add_tile(const char *image, Uint32 rows, Uint32 cols, Uint32 start_id);
      virtual Result set_data(const char *value);
      virtual void set_obj_type(const char *type);
      virtual const char* obj_type() const;
    protected:
      virtual Result build_level(const char *file);
      bool get_image_data(int d, const char **id, SDL_Rect **rc);
    public:
      virtual void doinit() override;
      virtual void update(Input &inp, Audio &aud) override;
      virtual void render(g2d::Renderer &r) override;
    protected:
      Sizei block_size_;
      Size tile_size_;
      std::vector<TileMapData> tile_map_datas_;
      std::vector<std::vector<Sprite*>> map_;
      std::string level_file_;
      Uint32 level_;
      std::string obj_type_;
  };
} // end namespace g2d

#endif // __GAME2D_CORE_TILEMAP_H__